﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcShop.Models;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.UI;
using System.IO;
//using Microsoft.Web.Mvc;
using MvcShop.MVC.Controllers;
using MvcShop.Extensions;
using MvcShop.Extensions.Html;
using MvcShop.MVC.Areas.Admin.Controllers;

namespace MvcShop.MVC.HtmlHelpers
{
    public enum CategoryDisplay
    {
        Edit,
        View
    }

    public class CategoryWriter
    {
        readonly IEnumerable<Category> _rootCategory;
        readonly UrlHelper _urlHelper;
        readonly CategoryDisplay _display;

        public CategoryWriter(IEnumerable<Category> rootCategory, UrlHelper urlHelper, CategoryDisplay display)
        {
            this._rootCategory = rootCategory;
            this._urlHelper = urlHelper;
            this._display = display;
        }

        public string Write()
        {
            var writer = new HtmlTextWriter(new StringWriter());

            if (_display == CategoryDisplay.View)
            {
                writer.AddAttribute("class", "category");
            }

            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            var rootCategory = _rootCategory.Where(c => c.ParentId == 1).OrderBy(c => c.Position);

            WriteCategories(writer, rootCategory);

            writer.RenderEndTag();

            return writer.InnerWriter.ToString();
        }

        private void WriteCategories(HtmlTextWriter writer, IEnumerable<Category> categories)
        {


            bool first = true;
            foreach (Category category in categories)
            {
                if (first)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                    first = false;
                }

                writer.RenderBeginTag(HtmlTextWriterTag.Li);
                writer.Write(WriteCategory(category));

                var nextCategory = _rootCategory.Where(c => c.ParentId == category.CategoryId).OrderBy(c => c.Position);
                WriteCategories(writer, nextCategory);
                writer.RenderEndTag();
            }

            if (!first) writer.RenderEndTag();
        }

        private string WriteCategory(Category category)
        {
            if (_display == CategoryDisplay.Edit)
            {
                return "{0} <a href=\"{1}\">修改</a> <a href=\"{2}\">删除</a> {3} {4} {5}".With(
                     WriteCategoryLink(category),
                    _urlHelper.Of<CategoriseManageController>().Action(c=>c.Edit,category.CategoryId),
                    _urlHelper.Of<CategoriseManageController>().Action(c => c.Delete, category.CategoryId),
                    category.CategoryId,
                    _urlHelper.ToCategoriesUpArrowLink(Convert.ToInt32(category.CategoryId)),
                    _urlHelper.ToCategoriesDownArrowLink(Convert.ToInt32(category.CategoryId))
                    );
            }

            return WriteCategoryLink(category);
        }

        private string WriteCategoryLink(Category category)
        {
            return "<a href=\"{0}\" target=\"_blank\">{1}</a> ".With(_urlHelper.ToListProduct(category.CategoryId), category.CategoryName);
        }
    }
}
